package com.ocean.core.util.decode;

import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.math.BigInteger;
import java.security.MessageDigest;

/**
 * 微信退款回调解密
 *
 * @author zhang
 * @date 2021-2-3
 */
public class DecodeUtil {

    public static String decode(String reqInfo, String key) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(key.getBytes());
        String keyMd5String = (new BigInteger(1, md5.digest())).toString(16).toLowerCase();
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyMd5String.getBytes(), "AES");
        cipher.init(2, secretKeySpec);
        return new String(cipher.doFinal(Base64.decodeBase64(reqInfo)));
    }
}
